package app.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;
import database.local.Deal;

public class CommentListActivity extends Activity {
	private Deal deal;
	String comment; 
	RatingBar moneyRate, starRate;
	EditText commentEditText;
	public void onCreate (Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comments_screen_layout);
		
		Bundle b = getIntent().getBundleExtra("values");
		deal = b.getParcelable("deal");
		
		//Open List
		ListView commentsLV = (ListView) findViewById(R.id.comments_list);
		commentsLV.setAdapter(new CommentsListArrayAdapter(this, deal.getComments()));
		
		//Rate
		commentEditText = (EditText) findViewById(R.id.comments_comment_edittext);
		
		moneyRate = (RatingBar) findViewById(R.id.comments_money_thr_ratingbar);
		moneyRate.setRating(1);
		starRate = (RatingBar) findViewById(R.id.comments_rating_thr_ratingbar);
		starRate.setRating(1);
		
		Button rateButton = (Button) findViewById(R.id.comments_rate_button);
		rateButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				rateDeal();
			}
		});
	}
	
	private void rateDeal(){
		comment = commentEditText.getText().toString();
		RateBusiness();
		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
		Toast.makeText(CommentListActivity.this, "Thank You, Comment sent to Save-Zone database", Toast.LENGTH_LONG);

		finish();
	}

	public void RateBusiness() {
		AndroAppActivity context = AndroAppActivity.getInstance();
		if (context == null){
			OpeningActivity context_o = OpeningActivity.getInstance();
			context_o.dbcc.rate(deal, (int) starRate.getRating(),
					(int) moneyRate.getRating(), comment);
			return;
		}
		context.dbc.rate(deal, (int) starRate.getRating(),
				(int) moneyRate.getRating(), comment);
		
	}
	
}
